package com.deke.home.service.impl;

import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.deke.entity.CrmBanner;
import com.deke.home.dao.CrmBannerDao;
import com.deke.home.service.CrmBannerService;
import com.deke.home.vo.CrmBannerVo;
import com.deke.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @author ：ludi（206913808@qq.com）
 * @date ：Created in 2022/12/6 18:56
 * @description：
 * @modified By：
 * @version:
 */
@Service
public class CrmBannerServiceImpl implements CrmBannerService {

    @Autowired
    private CrmBannerDao crmBannerDao;


    /**
     * @create by: ludi（206913808@qq.com）
     * @description: 分页查询、条件查询
     * @create time: 2022/12/10 14:59
     * @param current
     * @param pageSize
     * @param crmBannerVo
     * @return com.deke.vo.Result<com.baomidou.mybatisplus.core.metadata.IPage<com.deke.entity.CrmBanner>>
     */
    @Override
    public Result<IPage<CrmBanner>> queryByConditionPage(Integer current, Integer pageSize, CrmBannerVo crmBannerVo) {
        IPage<CrmBanner> page = new Page(current,pageSize);
        QueryWrapper<CrmBanner> wrapper = new QueryWrapper<>();
        wrapper.eq("is_deleted", 0);
        if (!StringUtils.isEmpty(crmBannerVo.getId())){
            wrapper.like("id",crmBannerVo.getId());
        }
        if (StringUtils.hasText(crmBannerVo.getTitle())){
            wrapper.like("title", crmBannerVo.getTitle());
        }
        if (StringUtils.hasText(crmBannerVo.getIsDisable())){
            wrapper.eq("is_disable", crmBannerVo.getIsDisable());
        }
        IPage<CrmBanner> iPage = crmBannerDao.selectPage(page, wrapper);
        return new Result<>(2000,"查询成功",iPage);
    }


    /**
     * @create by: ludi（206913808@qq.com）
     * @description: 改变状态
     * @create time: 2022/12/10 14:59
     * @param id
     * @param isDisable
     * @return com.deke.vo.Result
     */
    @Override
    public Result changeStatus(String id, String isDisable) {
        UpdateWrapper<CrmBanner> wrapper = new UpdateWrapper<>();
        wrapper.eq("id", id);
        CrmBanner crmBanner = new CrmBanner();
        crmBanner.setIsDisable(isDisable);
        crmBannerDao.update(crmBanner,wrapper);
        return new Result(2000,"状态改变成功");
    }


    /**
     * @create by: ludi（206913808@qq.com）
     * @description: 删除操作
     * @create time: 2022/12/10 17:07
     * @param id
     * @return com.deke.vo.Result
     */
    @Override
    public Result deleteBanner(String id) {
        UpdateWrapper<CrmBanner> wrapper = new UpdateWrapper<>();
        wrapper.eq("id", id);
        CrmBanner crmBanner = new CrmBanner();
        crmBanner.setIsDeleted("1");
        int update = crmBannerDao.update(crmBanner, wrapper);
        if (update==0){
            return new Result(2000, "删除成功");
        }else {
            return new Result(5000, "删除失败");
        }

    }



    /**
     * @create by: ludi（206913808@qq.com）
     * @description: 添加操作
     * @create time: 2022/12/10 17:33
     * @param crmBanner
     * @return com.deke.vo.Result
     */
    @Override
    public Result addBanner(CrmBanner crmBanner) {
        /**
         * 参数1为终端ID
         * 参数2为数据中心ID
         */
        if (ObjectUtil.isNotEmpty(crmBanner.getId())){
            UpdateWrapper<CrmBanner> wrapper = new UpdateWrapper<>();
            wrapper.eq("id", crmBanner.getId());
            int update = crmBannerDao.update(crmBanner, wrapper);
            if (update==0){
                return new Result(5000, "修改失败");
            }else {
                return new Result(2000, "修改成功");
            }
        }else{
            Snowflake snowflake = IdUtil.createSnowflake(1, 1);
            String nextIdStr = snowflake.nextIdStr();
            crmBanner.setId(nextIdStr);
            crmBanner.setIsDisable("0");
            int insert = crmBannerDao.insert(crmBanner);
            if (insert==0){
                return new Result(2000, "添加失败");
            }else {
                return new Result(5000, "添加成功");
            }
        }
    }

    /**
     * @create by: Username 刘亦辰 
     * @description:  查找前台的轮廓图 
     * @create time: 2022/12/21 16:52
     * @param null
     * @return :  
     **/
    @Override
    public Result getQianBanner() {
        QueryWrapper<CrmBanner> wrapper = new QueryWrapper<>();
        wrapper.eq("is_deleted",0);
        wrapper.eq("is_disable",0);
        List<CrmBanner> bannerList = crmBannerDao.selectList(wrapper);
        if (ObjectUtil.isNotEmpty(bannerList) && bannerList.size()>0){
            return new Result(2000,"成功",bannerList);
        }
        return new Result(0000,"失败");
    }
}